#ifndef __ADCReader_Detect_C
#define __ADCReader_Detect_C

#include "ADCReader.h"

void ADC_Init(void){
    ADC_Config_T    adcConfig;
	
    // Enable ADC clock 
	  RCM_EnableAPB2PeriphClock(ADC_RTC);  
	  	  
	  // ADCCLK = PCLK2/4 
	  #ifdef AT32F403AVGT7 
	  // AT32的ADC最大允许的时钟频率通常为28MHz，系统时钟240MHz,ADC最大20MHz。
	  // RCM_PCLK2_DIV_n = APB2时钟 120MHz / 20MHz = 6 =  RCM_PCLK2_DIV_6;
    RCM_ConfigADCCLK(RCM_PCLK2_DIV_8);  
	  #else
	  // APM32的ADC最大允许的时钟频率通常为14MHz。RCM_PCLK2_DIV_4 = APB2时钟 / 4 = 108 / 4 = 27
	  RCM_ConfigADCCLK(RCM_PCLK2_DIV_4);
	  #endif
        
    /* ADC configuration */ 
    #ifndef AT32F403AVGT7	
    ADC_Reset(ADC_Id);
    #endif	
	
    ADC_ConfigStructInit(&adcConfig);
    #if ADC_num > 1	
			#ifdef AT32F403AVGT7	
				adc_combine_mode_select(ADC_MODE_REG_SIMULT); 
			#else
				adcConfig.mode  = ADC_MODE_REG_SIMULT;
			#endif	
		#else
			#ifdef AT32F403AVGT7	
				adc_combine_mode_select(ADC_INDEPENDENT_MODE); 
			#else
				adcConfig.mode  = ADC_MODE_INDEPENDENT;
			#endif	
		#endif
	  
    adcConfig.continuosConvMode  = ENABLE;
    adcConfig.dataAlign          = ADC_DATA_ALIGN_RIGHT;
		#ifdef AT32F403AVGT7
		adc_ordinary_conversion_trigger_set(ADC_Id, ADC12_ORDINARY_TRIG_SOFTWARE, TRUE);
		#else
		adcConfig.externalTrigConv   = ADC_EXT_TRIG_CONV_None;
		#endif   
		
	  adcConfig.scanConvMode       = ENABLE;  //扫描模式 = DISABLE  ENABLE
	  // 通道总数：
    adcConfig.nbrOfChannel       = ADC_Total;  
    ADC_Config(ADC_Id, &adcConfig);

    #if UI_Meter_Method == Meter_SelfADC		
			ADC_ConfigRegularChannel(ADC_Id, ChgU_ADC_Chn, ADC_ChgU + 1, ADC_SAMPLETIME_CYCLES); 
      #if ADC_num == 1			
			ADC_ConfigRegularChannel(ADC_Id, ChgI_ADC_Chn, ADC_ChgI + 1, ADC_SAMPLETIME_CYCLES);
			#endif
    #endif		
		ADC_ConfigRegularChannel(ADC_Id, CP1_ADC_Chn, ADC_CP + 1, ADC_SAMPLETIME_CYCLES);			
		#if ADC_num == 1
	    ADC_ConfigRegularChannel(ADC_Id, TEMP1_ADC_Chn, ADC_TEMPR_BoardIn + 1, ADC_SAMPLETIME_CYCLES);
	    #ifdef TEMP2_ADC_Chn
	    ADC_ConfigRegularChannel(ADC_Id, TEMP2_ADC_Chn, ADC_TEMPR_External + 1, ADC_SAMPLETIME_CYCLES);
	    #endif
	    #ifdef TEMP3_ADC_Chn
	    ADC_ConfigRegularChannel(ADC_Id, TEMP3_ADC_Chn, ADC_TEMPR_ChgGun + 1, ADC_SAMPLETIME_CYCLES);
	    #endif
      #if PE_method == PE_method_ADC
		  ADC_ConfigRegularChannel(ADC_Id, PE_ADC_Chn, ADC_PE + 1, ADC_SAMPLETIME_CYCLES);
		  #endif			
    #elif ADC_num == 2
      #ifdef  TEMP2_ADC_Chn
				ADC_ConfigRegularChannel(ADC_Id, TEMP2_ADC_Chn, ADC_TEMPR_External + 1, ADC_SAMPLETIME_CYCLES);
	    #else
				#ifdef  TEMP3_ADC_Chn
				ADC_ConfigRegularChannel(ADC_Id, TEMP3_ADC_Chn, ADC_TEMPR_ChgGun + 1, ADC_SAMPLETIME_CYCLES);
				#endif
      #endif
	  #endif
		
		// 采样时间 = (239.5+12.5) / (96 / 8)	= 21us = 95238 kHZ    100us采样一次即可；
		// 采样时间 = (72.5+12.5) / (96 / 8)	= 7us  = 95238 kHZ    100us采样一次即可；
		
		// 使能DMA通道;
    ADC_EnableDMA(ADC_Id);
		
		/* Enable ADC */
		#ifdef AT32F403AVGT7	
		adc_enable(ADC_Id, TRUE);
		#else
    ADC_Enable(ADC_Id);	
		#endif
		
		// 校准
    ADC_ResetCalibration(ADC_Id);		
    while(ADC_ReadResetCalibrationStatus(ADC_Id));
    ADC_StartCalibration(ADC_Id);
    while(ADC_ReadCalibrationStartFlag(ADC_Id));
		
		// 启动方式设置：		
		#ifdef AT32F403AVGT7
		adc_ordinary_software_trigger_enable(ADC_Id, TRUE);
    #else		
    ADC_EnableSoftwareStartConv(ADC_Id);    
		#endif	
		
}

#if ADC_num > 1
void ADC2_Init(void){
    ADC_Config_T    adcConfig;
	
    // Enable ADC clock 
	  RCM_EnableAPB2PeriphClock(ADC2_RTC);  
	  
	  // ADCCLK = PCLK2/4 
	  #ifdef AT32F403AVGT7 
	  // AT32的ADC最大允许的时钟频率通常为28MHz，系统时钟240MHz,ADC最大20MHz。
	  // RCM_PCLK2_DIV_n = APB2时钟 120MHz / 20MHz = 6 =  RCM_PCLK2_DIV_6;
    RCM_ConfigADCCLK(RCM_PCLK2_DIV_6);  
	  #else
	  // APM32的ADC最大允许的时钟频率通常为14MHz。RCM_PCLK2_DIV_4 = APB2时钟 / 4 = 108 / 4 = 27
	  RCM_ConfigADCCLK(RCM_PCLK2_DIV_4);
	  #endif  
    
    // ADC configuration  
    #ifndef AT32F403AVGT7	
    ADC_Reset(ADC2_Id);
    #endif	
	
    ADC_ConfigStructInit(&adcConfig);		
    #ifdef AT32F403AVGT7	
			adc_combine_mode_select(ADC_MODE_REG_SIMULT); 
		#else
			adcConfig.mode  = ADC_MODE_REG_SIMULT;
		#endif	

    adcConfig.continuosConvMode  = ENABLE;
    adcConfig.dataAlign          = ADC_DATA_ALIGN_RIGHT;
		#ifdef AT32F403AVGT7
		adc_ordinary_conversion_trigger_set(ADC2_Id, ADC12_ORDINARY_TRIG_SOFTWARE, TRUE);
		#else
		adcConfig.externalTrigConv   = ADC_EXT_TRIG_CONV_None;
		#endif   
		
	  adcConfig.scanConvMode       = ENABLE;  //扫描模式 = DISABLE  ENABLE
	  // 通道总数：
    adcConfig.nbrOfChannel       = ADC2_Total;  
    ADC_Config(ADC2_Id, &adcConfig);
		// 通道序列配置：
		#if UI_Meter_Method == Meter_SelfADC
	  ADC_ConfigRegularChannel(ADC2_Id, ChgI_ADC_Chn, ADC_ChgI + 1, ADC_SAMPLETIME_CYCLES);
	  #endif
	  ADC_ConfigRegularChannel(ADC2_Id, TEMP1_ADC_Chn, ADC_TEMPR_BoardIn + 1, ADC_SAMPLETIME_CYCLES);
		#if ADC_num == 2
	    #ifdef  TEMP2_ADC_Chn
				#ifdef  TEMP3_ADC_Chn
				ADC_ConfigRegularChannel(ADC2_Id, TEMP3_ADC_Chn, ADC_TEMPR_ChgGun + 1, ADC_SAMPLETIME_CYCLES);
	      #else
	      ADC_ConfigRegularChannel(ADC2_Id, DMA_ADC_Core_TEMP_Chn, ADC_TEMPR_MCU + 1, ADC_SAMPLETIME_CYCLES);
				#endif
	    #else
				#ifdef  TEMP3_ADC_Chn				
	      ADC_ConfigRegularChannel(ADC2_Id, DMA_ADC_Core_TEMP_Chn, ADC_TEMPR_MCU + 1, ADC_SAMPLETIME_CYCLES);
				#endif
      #endif
	  #endif
		
		/* Enable ADC */
		#ifdef AT32F403AVGT7	
		adc_enable(ADC2_Id, TRUE);
		#else
    ADC_Enable(ADC2_Id);	
		#endif
		
		// 校准
    ADC_ResetCalibration(ADC2_Id);		
    while(ADC_ReadResetCalibrationStatus(ADC2_Id)); 
    ADC_StartCalibration(ADC2_Id); 
    while(ADC_ReadCalibrationStartFlag(ADC2_Id)); 
		
		// 启动方式设置：		
		#ifdef AT32F403AVGT7
		//adc_ordinary_software_trigger_enable(ADC2_Id, TRUE);
    #else		
    // ADC_EnableSoftwareStartConv(ADC2_Id);
    ADC_EnableExternalTrigConv(ADC2_Id);	    
		#endif	
}
#endif


#if ADC_num > 2 && (defined TEMP2_ADC_Chn || defined TEMP3_ADC_Chn)
void ADC3_Init(void){
    ADC_Config_T    adcConfig;
	
    // Enable ADC clock 
	  RCM_EnableAPB2PeriphClock(ADC3_RTC);  
	  
	  // ADCCLK = PCLK2/4 
	  #ifdef AT32F403AVGT7 
	  // AT32的ADC最大允许的时钟频率通常为28MHz，系统时钟240MHz,ADC最大20MHz。
	  // RCM_PCLK2_DIV_n = APB2时钟 120MHz / 20MHz = 6 =  RCM_PCLK2_DIV_6;
    RCM_ConfigADCCLK(RCM_PCLK2_DIV_6);  
	  #else
	  // APM32的ADC最大允许的时钟频率通常为14MHz。RCM_PCLK2_DIV_4 = APB2时钟 / 4 = 108 / 4 = 27
	  RCM_ConfigADCCLK(RCM_PCLK2_DIV_4);
	  #endif  
    
    // ADC configuration  
    #ifndef AT32F403AVGT7	
    ADC_Reset(ADC3_Id);
    #endif	
	  
    ADC_ConfigStructInit(&adcConfig);
    #ifdef AT32F403AVGT7	
			adc_combine_mode_select(ADC_MODE_INDEPENDENT); 
		#else
			adcConfig.mode  = ADC_MODE_INDEPENDENT;
		#endif	  
    adcConfig.continuosConvMode  = ENABLE;
    adcConfig.dataAlign          = ADC_DATA_ALIGN_RIGHT;
		#ifdef AT32F403AVGT7
		adc_ordinary_conversion_trigger_set(ADC3_Id, ADC3_ORDINARY_TRIG_SOFTWARE, TRUE);
		#else
		adcConfig.externalTrigConv   = ADC_EXT_TRIG_CONV_None;
		#endif   
		
	  adcConfig.scanConvMode       = ENABLE;  //扫描模式 = DISABLE  ENABLE
	  // 通道总数：
    adcConfig.nbrOfChannel       = ADC3_Total;  
    ADC_Config(ADC3_Id, &adcConfig);
		
		// 通道序列配置：
		#ifdef  TEMP2_ADC_Chn
		ADC_ConfigRegularChannel(ADC3_Id, TEMP2_ADC_Chn, ADC_TEMPR_External + 1, ADC_SAMPLETIME_CYCLES);
		#endif 	
	  #ifdef  TEMP3_ADC_Chn
		ADC_ConfigRegularChannel(ADC3_Id, TEMP3_ADC_Chn, ADC_TEMPR_ChgGun + 1, ADC_SAMPLETIME_CYCLES);
    #endif	
		
		// 采样时间 = (239.5+12.5) / (96 / 8)	= 21us = 95238 kHZ    100us采样一次即可；
		// 采样时间 = (72.5+12.5) / (96 / 8)	= 7us  = 95238 kHZ    100us采样一次即可；
		// 使能DMA通道;
    ADC_EnableDMA(ADC3_Id);
		
		/* Enable ADC */
		#ifdef AT32F403AVGT7	
		adc_enable(ADC3_Id, TRUE);
		#else
    ADC_Enable(ADC3_Id);	
		#endif
		
		// 校准
    ADC_ResetCalibration(ADC3_Id);		
    while(ADC_ReadResetCalibrationStatus(ADC3_Id)); 
    ADC_StartCalibration(ADC3_Id); 
    while(ADC_ReadCalibrationStartFlag(ADC3_Id)); 
		
		// 启动方式设置：		
		#ifdef AT32F403AVGT7
		adc_ordinary_software_trigger_enable(ADC3_Id, TRUE);
    #else		
    ADC_EnableSoftwareStartConv(ADC3_Id);    	
		#endif	
}
#endif


// 数据搬运：一次搬运1个采样数据，16bit或32bit。
void DMA_Init(){
    DMA_Config_T dmaconfig;
    RCM_EnableAHBPeriphClock(DMA_ADC_RTC);
    #ifdef AT32F403AVGT7	
	  // nvic_irq_enable(DMA1_Channel1_IRQn, 0, 0);	//DMA中断
    dma_reset(DMA_ADC_Chn);
	  dma_default_para_init(&dmaconfig);
    #endif	
    dmaconfig.bufferSize         = ADC_Total;
    dmaconfig.dir                = DMA_DIR_PERIPHERAL_SRC;
    dmaconfig.loopMode           = DMA_MODE_CIRCULAR;
    //dmaconfig.memoryBaseAddr     = (uint32_t)ADC_Value;
	  #if ADC_num > 2
	  dmaconfig.memoryBaseAddr     = (uint32_t)ADC_Buffer.ADC_Array.ADC_Value;
	  #else
	  dmaconfig.memoryBaseAddr     = (uint32_t)ADC_Buffer.ADC_Value; 
	  #endif	  
	  #if ADC_num > 1
    dmaconfig.memoryDataSize     = DMA_MEMORY_DATA_SIZE_WOED; 
	  #else
	  dmaconfig.memoryDataSize     = DMA_MEMORY_DATA_SIZE_HALFWORD;
	  #endif		
	  dmaconfig.memoryInc          = DMA_MEMORY_INC_ENABLE;  // 允许内存地址递增
    dmaconfig.peripheralBaseAddr = (uint32_t)&ADC_Id -> REGDATA; //((uint32_t)ADC1_BASE + 0x4C)
	  #if ADC_num > 1
    dmaconfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_WOED;
	  #else
		dmaconfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_HALFWORD;
		#endif
	  dmaconfig.peripheralInc      = DMA_PERIPHERAL_INC_DISABLE; // 禁止引脚递增
    dmaconfig.priority           = DMA_PRIORITY_HIGH;
	  #ifndef AT32F403AVGT7	
    dmaconfig.M2M                = DMA_M2MEN_DISABLE;
    #endif
		
    DMA_Config(DMA_ADC_Chn, &dmaconfig);	
    DMA_Enable(DMA_ADC_Chn);
		
}

// Buf内存
#if ADC_num > 2 && (defined TEMP2_ADC_Chn || defined TEMP3_ADC_Chn)
void DMA2_ADC3_Init(){
    DMA_Config_T dmaconfig;
    RCM_EnableAHBPeriphClock(DMA2_ADC3_RTC);
    #ifdef AT32F403AVGT7	
    dma_reset(DMA2_ADC3_Chn);
	  dma_default_para_init(&dmaconfig);
    #endif	
    dmaconfig.bufferSize         = ADC3_Total;
    dmaconfig.dir                = DMA_DIR_PERIPHERAL_SRC;
    dmaconfig.loopMode           = DMA_MODE_CIRCULAR;
    //dmaconfig.memoryBaseAddr     = (uint32_t)ADC_Value;
	  dmaconfig.memoryBaseAddr     = (uint32_t)ADC_Buffer.ADC_Array.ADC3_Value;
    dmaconfig.memoryDataSize     = DMA_MEMORY_DATA_SIZE_HALFWORD;
	  dmaconfig.memoryInc          = DMA_MEMORY_INC_ENABLE;  // 允许内存地址递增
    dmaconfig.peripheralBaseAddr = (uint32_t)&ADC3_Id -> REGDATA; //((uint32_t)ADC1_BASE + 0x4C)
    dmaconfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_HALFWORD;
	  dmaconfig.peripheralInc      = DMA_PERIPHERAL_INC_DISABLE; // 禁止引脚递增
    dmaconfig.priority           = DMA_PRIORITY_HIGH;
	  #ifndef AT32F403AVGT7	
    dmaconfig.M2M                = DMA_M2MEN_DISABLE;
    #endif
	
    DMA_Config(DMA2_ADC3_Chn, &dmaconfig);	
    DMA_Enable(DMA2_ADC3_Chn);
}
#endif


#endif

